package com.example.myprojectoffeixue;

/**
 * Created by ASUS on 2016/12/27.
 */
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
/**
 * 自定义滑动开关
 */
public class MySwitchButton extends View implements OnTouchListener{
    int width;
    View parentView;
    int lastX;
    private int isSwitchOn=0;
    int offX;
    // boolean isMove;
    private ArrayList<OnSwitchListener> onSwitchListenerList; // 开关监听器
    public MySwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
    public MySwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        this.setOnTouchListener(this); // 设置触摸监听器
        onSwitchListenerList = new ArrayList<>();
    }

    public void setParentView(View parentView) {
        this.parentView = parentView;
        parentView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){


                    int downX= (int) motionEvent.getX();
                    if(downX>0&&downX<getMeasuredWidth()){
                        isSwitchOn=0;
                        layout(0, getTop(),getMeasuredWidth(), getBottom());
                    }else if(downX>getMeasuredWidth()&&downX<getMeasuredWidth()*2){
                        isSwitchOn=1;
                        layout(getMeasuredWidth(), getTop(),getMeasuredWidth()*2 , getBottom());
                    }else if(downX>getMeasuredWidth()*2&&downX<getMeasuredWidth()*3){
                        isSwitchOn=2;
                        layout(getMeasuredWidth()*2, getTop(),getMeasuredWidth()*3, getBottom());

                    }
                    if(onSwitchListenerList.size() > 0){
                        for(OnSwitchListener listener : onSwitchListenerList){
                            listener.onSwitched(isSwitchOn); } }
                }
                return true;
            }
        });
    }

    public MySwitchButton(Context context) {
        super(context);
    }
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        view.setLongClickable(false);
        int x = (int) event.getX();
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                break;
          /*  case MotionEvent.ACTION_MOVE:
                offX = x - lastX;
                width = getMeasuredWidth();
                if(getLeft()+offX>0&&getRight()+offX<width*3) {
                    layout(getLeft() + offX, getTop(), getRight() + offX, getBottom());
                }
                break;*/
            case MotionEvent.ACTION_UP:

                int previousState2 = isSwitchOn;
                int upX2 = (int) event.getRawX();
                int left2 = upX2 - parentView.getLeft();
                width = getMeasuredWidth();
                if (left2 > 0 && left2 < width) {
                    //开
                    layout(0, getTop(), width, getBottom());
                } else if (left2 > width && left2 < width * 2) {
                    //停
                    layout(width, getTop(), width * 2, getBottom());
                } else if (left2 > width * 2 && left2 < width * 3) {
                    //关
                    layout(width * 2, getTop(), width * 3, getBottom());
                }
                if(getLeft()==0){
                    isSwitchOn = 0;
                }else if(getLeft()==width*2){
                    isSwitchOn = 2;
                }else{
                    isSwitchOn = 1;
                }

                if (previousState2!= isSwitchOn) {

                    if (onSwitchListenerList.size() > 0) {
                        for (OnSwitchListener listener : onSwitchListenerList) {
                            listener.onSwitched(isSwitchOn);
                        }


                    }
                }
                break;

        }


        return  true;
    }

    public void setOnSwitchStateListener(OnSwitchListener listener){
        onSwitchListenerList.add(listener); }
}
